/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.utilities;

import java.text.DecimalFormat;

/**
 * The Class TimeUtilities.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class TimeUtilities {

	 /** The Constant MILLIS_NANOS. */
	 public static final long MILLIS_NANOS = new Long(1000000).longValue();

	 /** The Constant SECOND_NANOS. */
	 public static final long SECOND_NANOS = TimeUtilities.MILLIS_NANOS * new Long(1000).longValue();

	 /** The Constant MINUTE_NANOS. */
	 public static final long MINUTE_NANOS = TimeUtilities.SECOND_NANOS * new Long(60).longValue();

	 /** The Constant HOUR_NANOS. */
	 public static final long HOUR_NANOS = TimeUtilities.MINUTE_NANOS * new Long(60).longValue();

	 /** The Constant MILLIS_UNIT. */
	 public static final String MILLIS_UNIT = "ms";

	 /** The Constant SECOND_UNIT. */
	 public static final String SECOND_UNIT = "s";

	 /** The Constant MINUTE_UNIT. */
	 public static final String MINUTE_UNIT = "m";

	 /** The Constant HOUR_UNIT. */
	 public static final String HOUR_UNIT = "h";

	 /**
	  * From nanos to unit.
	  *
	  * @param nanos the nanos
	  *
	  * @return the string
	  */
	 public static String fromNanosToUnit(double nanos) {
			return TimeUtilities.fromNanosToUnit(new Double(nanos).longValue());
	 }

	 /**
	  * From nanos to unit.
	  *
	  * @param nanos the nanos
	  *
	  * @return the string
	  */
	 public static String fromNanosToUnit(long nanos) {

			nanos = Math.abs(nanos);

			if (nanos < TimeUtilities.SECOND_NANOS) {
				 return TimeUtilities.MILLIS_UNIT;
			} else if (nanos < TimeUtilities.MINUTE_NANOS) {
				 return TimeUtilities.SECOND_UNIT;
			} else if (nanos < TimeUtilities.HOUR_NANOS) {
				 return TimeUtilities.MINUTE_UNIT;
			} else {
				 return TimeUtilities.HOUR_UNIT;
			}
	 }

	 /**
	  * From nanos to value.
	  *
	  * @param nanos the nanos
	  *
	  * @return the double
	  */
	 public static double fromNanosToValue(double nanos) {
			return TimeUtilities.fromNanosToValue(nanos, TimeUtilities.fromNanosToUnit(nanos));
	 }

	 /**
	  * From nanos to value.
	  *
	  * @param nanos the nanos
	  * @param unit the unit
	  *
	  * @return the double
	  */
	 public static double fromNanosToValue(double nanos, String unit) {

			if (unit.equals(TimeUtilities.MILLIS_UNIT)) {
				 return nanos / TimeUtilities.MILLIS_NANOS;
			} else if (unit.equals(TimeUtilities.SECOND_UNIT)) {
				 return nanos / TimeUtilities.SECOND_NANOS;
			} else if (unit.equals(TimeUtilities.MINUTE_UNIT)) {
				 return nanos / TimeUtilities.MINUTE_NANOS;
			} else if (unit.equals(TimeUtilities.HOUR_UNIT)) {
				 return nanos / TimeUtilities.HOUR_NANOS;
			}

			return nanos;
	 }

	 /**
	  * From nanos to string.
	  *
	  * @param nanos the nanos
	  *
	  * @return the string
	  */
	 public static String fromNanosToString(double nanos) {
			final DecimalFormat decimalFormat = new DecimalFormat("###,###,###,##0.##");
			return TimeUtilities.fromNanosToString(nanos, decimalFormat);

	 }

	 /**
	  * From nanos to string.
	  *
	  * @param nanos the nanos
	  * @param decimalFormat the decimal format
	  *
	  * @return the string
	  */
	 public static String fromNanosToString(double nanos, DecimalFormat decimalFormat) {
			return decimalFormat.format(TimeUtilities.fromNanosToValue(nanos)) + " " + TimeUtilities.fromNanosToUnit(nanos);
	 }
}
